package com.studysystem.repository;

import com.studysystem.entity.CourseReview;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface CourseReviewRepository extends JpaRepository<CourseReview, Long> {
    List<CourseReview> findByCourseId(Long courseId);
    Optional<CourseReview> findByUserIdAndCourseId(Long userId, Long courseId);
    Page<CourseReview> findByCourseId(Long courseId, Pageable pageable);
    
    @Query("SELECT AVG(r.rating) FROM CourseReview r WHERE r.course.id = ?1")
    Double getAverageRatingByCourseId(Long courseId);
    
    @Query("SELECT COUNT(r) FROM CourseReview r WHERE r.course.id = ?1")
    Long getReviewCountByCourseId(Long courseId);
} 